package com.selune.online.ordering.system.project.tenant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.pojo.goods.dto.GoodsDTO;
import com.selune.online.ordering.system.project.api.pojo.goods.dto.GoodsSearchDTO;
import com.selune.online.ordering.system.project.api.pojo.goods.dto.GoodsUpdateDTO;
import com.selune.online.ordering.system.project.api.pojo.goods.vo.GoodsDetailVO;
import com.selune.online.ordering.system.project.api.service.tenant.ITenantGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户商品信息控制器
 *
 * @author xiaoyp
 * @date 2020/2/27
 */

@RestController
@RequestMapping("/api/v1/ordering/tenant/goods")
@Slf4j
@Api(value = "商户商品接口")
public class TenantGoodsController {
	
	@Autowired
	private ITenantGoodsService iTenantGoodsService;
	
	@ApiOperation(value = "添加商品", notes = "添加商品")
	@ApiImplicitParam(name = "goodsDTO", value = "保存商品信息DTO", required = true, dataType = "GoodsDTO", paramType = "body")
	@PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> saveGoods(@RequestBody GoodsDTO goodsDTO) {
		
		return iTenantGoodsService.saveGoods(goodsDTO);
	}
	
	@ApiOperation(value = "修改商品信息", notes = "修改商品信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "goodsUpdateDTO", value = "商品修改DTO", required = true, dataType = "GoodsUpdateDTO", paramType = "body")
	})
	@PutMapping(value = "/update/{goodsId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> updateGoods(@PathVariable(value = "goodsId") Long goodsId,
	                                     @RequestBody GoodsUpdateDTO goodsUpdateDTO) {
		return iTenantGoodsService.updateGoods(goodsId, goodsUpdateDTO);
	}
	
	@ApiOperation(value = "获取商品详情信息", notes = "获取商品详情信息")
	@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Long", paramType = "query")
	@GetMapping(value = "/detail/{goodsId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<GoodsDetailVO> getGoodsDetail(@PathVariable(value = "goodsId") Long goodsId) {
		
		return iTenantGoodsService.getGoodsDetail(goodsId);
	}
	
	@ApiOperation(value = "商品上架", notes = "商品上架")
	@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Long", paramType = "query")
	@PutMapping(value = "/sale/on/{goodsId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> onSaleGoods(@PathVariable(value = "goodsId") Long goodsId) {
		
		return iTenantGoodsService.onSaleGoods(goodsId);
	}
	
	@ApiOperation(value = "商品下架", notes = "商品下架")
	@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Long", paramType = "query")
	@PutMapping(value = "/sale/off/{goodsId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> offSaleGoods(@PathVariable(value = "goodsId") Long goodsId) {
		
		return iTenantGoodsService.offSaleGoods(goodsId);
	}
	
	@ApiOperation(value = "商品列表", notes = "商品列表")
	@ApiImplicitParam(name = "goodsSearchDTO", value = "商品查询条件DTO", dataType = "GoodsSearchDTO", paramType = "query")
	@GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<List<GoodsDetailVO>> listGoods(GoodsSearchDTO goodsSearchDTO) {
		
		return iTenantGoodsService.listGoods(goodsSearchDTO);
	}
	
	@ApiOperation(value = "商品分页", notes = "商品分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "current", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
		@ApiImplicitParam(name = "size", value = "页大小", dataType = "Integer", paramType = "query"),
		@ApiImplicitParam(name = "goodsSearchDTO", value = "商品查询条件", dataType = "GoodsSearchDTO", paramType = "query")
	})
	@GetMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Page<GoodsDetailVO>> pageGoods(Integer current, Integer size,
	                                               GoodsSearchDTO goodsSearchDTO) {
		return iTenantGoodsService.pageGoods(current, size, goodsSearchDTO);
	}
}
